package com.example.bitcoinconverter;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText text;
	private TextView rateText;
	private EditText setRateText;
	private Date date;
	private EditText setDateText;
	private TextView dateText;
	private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (EditText) findViewById(R.id.editText1);
		rateText = (TextView) findViewById(R.id.textView1);
		rateText.setText("PLN/Bitcoin rate: " + Float.toString(ConverterUtil.getExchangeRate()));
		setRateText = (EditText) findViewById(R.id.editText2);
		dateText = (TextView) findViewById(R.id.textView2);
		try
		{
			dateText.setText("Rate date: " + sdf.parse("14-01-2014"));
		}
		catch(ParseException e)
		{
			dateText.setText("Rate date: 14-01-2014");
		}
		setDateText = (EditText) findViewById(R.id.editText3);
	}
	
	// this method is called at button click because we assigned the name to the
	  // "OnClick property" of the button
	  
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreateContextMenu", 1).show();
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public CharSequence onCreateDescription() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreateDescription", 1).show();
		return super.onCreateDescription();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreateOptionsMenu", 1).show();
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onCreatePanelMenu(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreatePanelMenu", 1).show();
		return super.onCreatePanelMenu(featureId, menu);
	}

	@Override
	public View onCreatePanelView(int featureId) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreatePanelView", 1).show();
		return super.onCreatePanelView(featureId);
	}

	@Override
	public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreateThumbnail", 1).show();
		return super.onCreateThumbnail(outBitmap, canvas);
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreateDialog", 1).show();
		return super.onCreateDialog(id, args);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onMenuOpened", 1).show();
		return super.onMenuOpened(featureId, menu);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onCreateDialog", 1).show();
		return super.onCreateDialog(id);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onDestroy", 1).show();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onPause", 1).show();
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onRestart", 1).show();
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onResume", 1).show();
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onSaveInstanceState", 1).show();
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onStart", 1).show();
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "onStop", 1).show();
		super.onStop();
	}

	public void onClick(View view) 
	{
		switch (view.getId()) 
	    {
			case R.id.button1:
	    	RadioButton plnButton = (RadioButton) findViewById(R.id.radio0);
	    	RadioButton bitcoinButton = (RadioButton) findViewById(R.id.radio1);
	    	if (text.getText().length() == 0) 
	    	{
	    		Toast.makeText(this, "Please enter a valid number",Toast.LENGTH_LONG).show();
	    		return;
	    	}
	    	
	    	float inputValue = Float.parseFloat(text.getText().toString());
	    	
	    	if (plnButton.isChecked()) 
	    	{
	    		text.setText(String.valueOf(ConverterUtil.convertBitcoinToPLN(inputValue)));
	    		plnButton.setChecked(false);
	    		bitcoinButton.setChecked(true);
	    	} 
	    	else 
	    	{
	    		text.setText(String
	            .valueOf(ConverterUtil.convertPLNToBitcoin(inputValue)));
	    		bitcoinButton.setChecked(false);
	    		plnButton.setChecked(true);
	    	}
	    	break;
	    }
	}
	public void onClickSetRate(View view) 
	{
		switch (view.getId()) 
	    {
			case R.id.button2:
	    	
	    	if (setRateText.getText().length() == 0) 
	    	{
	    		Toast.makeText(this, "Please enter a valid number",Toast.LENGTH_LONG).show();
	    		return;
	    	}
	    	
	    	float inputValue = Float.parseFloat(setRateText.getText().toString());
	    	
	    	ConverterUtil.setExchangeRate(inputValue);
	    	rateText.setText("PLN/Bitcoin rate: " + Float.toString(inputValue));
	    	break;
	    }
	}
	public void onClickSetDate(View view) 
	{
		switch (view.getId()) 
	    {
			case R.id.button3:
	    	
	    	if (setDateText.getText().length() == 0) 
	    	{
	    		Toast.makeText(this, "Please enter a valid date",Toast.LENGTH_LONG).show();
	    		return;
	    	}
	    	
	    	String dateToValidate = setDateText.getText().toString();
	    	
	    	//String dateFormat = new String("YYYY-MM-DD");
			
			sdf.setLenient(false);
	 
			try 
			{	 
				//if not valid, it will throw ParseException
				date = sdf.parse(dateToValidate);
				System.out.println(date);	 
			} 
			catch (ParseException e) 
			{
	 
				Toast.makeText(this, "Invalid date format",Toast.LENGTH_LONG).show();
				return;
			}
	    	
	    	ConverterUtil.setDate(date);
	    	dateText.setText("Rate date: " + date.toString());
	    	break;
	    }
	}
	 /*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	*/

}
